package com.mef.smsplat.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.BaseJdbcDao;
import com.mef.smsplat.dao.base.BaseSpringJdbcDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.PersonInfoDomain;

@Repository
public class PersonInfoDao extends BaseSpringJdbcDao<PersonInfoDomain, String> {
	
	/**
	 * 根据用户名分页查询操作员信息
	 * @param personName
	 * @param personNi
	 * @param personPhone
	 * @param dgm
	 * @return
	 */
	public Page query(String personName,String personNi,String personPhone,String groupId,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from person_info p left join group_menber_info g on p.person_id=g.menber_id where 1=1");
		if(personName!=null&&!"".equals(personName)){
			sql.append(" and p.person_name like ?");
			params.add("%"+personName+"%");
		}
		if(personNi!=null&&!"".equals(personNi)){
			sql.append(" and p.person_ni like ?");
			params.add("%"+personNi+"%");
		}
		if(personPhone!=null&&!"".equals(personPhone)){
			sql.append(" and p.person_phone like ?");
			params.add("%"+personPhone+"%");
		}
		if(groupId!=null&&!"".equals(groupId)){
			sql.append(" and g.group_id=?");
			params.add(groupId);
		}
		Page<PersonInfoDomain> page= super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
		return page;
	}
	
	/**
	 * 根据身份证号和id查询是否唯一
	 * @param personPhone
	 * @param id
	 * @return
	 */
	public boolean queryByPhoneAndId(String personPhone,String id){
		String sql="select * from person_info t where t.person_phone=? and t.person_id!=?";
		List<PersonInfoDomain> list=super.query(sql, new Object[]{personPhone,id});
		if(list.size()>0)
			return false;
		return true;
	}

	@Override
	public BaseJdbcDao getDaoClass() {
		return this;
	}

	@Override
	public Class getEntityClass() {
		return PersonInfoDomain.class;
	}
	
	/**
	 * 批量处理
	 * @param ids
	 * @return
	 */
	public void deletePersonMember(String[] ids){
		String sql="delete from group_menber_info where menber_id=?";
		for (int i = 0; i < ids.length; i++) {
			super.getJdbcTemplate().update(sql, new Object[]{ids[i]});
		}
	}
	
	/**
	 * 根据组id 分页查询成员信息
	 * @param groupId
	 * @param dgm
	 * @return
	 */
	public Page queryMember(String groupId,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from person_info p left join group_menber_info g on p.person_id=g.menber_id where g.group_id=?");
		params.add(groupId);
		Page<PersonInfoDomain> page= super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
		return page;
	}

}
